/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.itintegration.catalogue.mek.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;
import se.sll.service.catalogue.common.AbstractJpaFacade;
import se.sll.service.catalogue.idmap.entity.VMEntity;

/**
 *
 * @author khaleddaham
 */
@Slf4j
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class VMEjb extends AbstractJpaFacade<VMEntity> {

    @PersistenceContext(unitName = "idmapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return this.em;
    }

    @Override
    protected void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public VMEjb() {
        super(VMEntity.class);
    }

    /**
     *
     * @param vmId
     * @param vmCareGiverPartId
     * @return
     */
    public List<VMEntity> findVmCareGiver(String vmId, String vmCareGiverPartId) {
        javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery<VMEntity> cq = cb.createQuery(VMEntity.class);
        javax.persistence.criteria.Root<VMEntity> table = cq.from(VMEntity.class);
        cq.where(
                cb.equal(table.get("vmId"), vmId),
                cb.equal(table.get("vmCareGiverPartID"), vmCareGiverPartId)
        );
        javax.persistence.TypedQuery<VMEntity> query = getEntityManager().createQuery(cq);
        return query.getResultList();
    }

    /**
     * 
     * @return 
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<VMEntity> findAll() {
        return this.em.createQuery("SELECT v FROM VMEntity v JOIN FETCH v.hsaId").getResultList();
    }
}
